package demo.practice.tags.binary_search;

public class P704 {

    public static void main(String[] args) {
        P704 p704= new P704();
        int search = p704.search(new int[]{-1, 0, 3, 5, 6, 9, 12}, 2);//测试用例必须满足有序
        System.out.println(search);
    }

    public int search(int[] nums, int target) {
        if(nums==null|| nums.length==0)
            return -1;
        if(nums[0]==target)
            return 0;
        if(nums[nums.length-1]==target)
            return nums.length-1;
        return search(nums,target,0,nums.length-1);
    }

    public int search(int[] nums, int target,int min,int max) {

        if(min>max)
            return -1;
        int middle= min+(max-min)/2;
        if(nums[middle]==target)
            return middle;

        if(nums[middle]>target)//目标值在前面
            return search(nums,target,min,middle-1);
        else
            //目标在后面
            return search(nums,target,middle+1,max);

    }
}
